import { observable, action, toJS } from 'mobx';
import * as DispatchSetAPI from '../apis/dispatchSet'
import { WeaForm ,WeaTableNew} from 'comsMobx';
const {TableStore} = WeaTableNew;

export default class DispatchSet{

	//规则列表
	@observable tableStore = new TableStore();
	/** 列表store */
	@observable title = '';
	@observable loading = false; // 加载中状态
	@observable sessionkey = ''; // 表格sessionkey

	//规则明细列表
	@observable tableStore1 = new TableStore();
	@observable sessionkey1 = ''; // 表格sessionkey

	/** 高级查询store */
	@observable form = new WeaForm(); // 表单
	@observable conditioninfo = []; // 高级搜索条件

	@observable dialogType='';
	@observable dialogTypeDeatil='';
	@observable hasRight=true;
	@observable showSearchAd = false;
	@observable conditionLoading = false;
	@observable quickSearchValue = '';
    @observable visible = false;
	@observable visible1 = false;
    @observable dialogtitle = '';
    @observable rightMenu = [];//页面右键

    @observable form1 = new WeaForm();//新增或编辑规则
    @observable chooseDialogCon1 = [];//新增或编辑规则字段
	@observable chooseDialogRM1 = [];//弹出框右键

    @observable form2 = new WeaForm();//新增或编辑规则明细
    @observable chooseDialogCon2 = [];//新增或编辑规则明细字段
	@observable chooseDialogRM2 = [];//弹出框右键

    @observable ruleid = "";
    @observable ruledetailid = "";


	@action doSearch(params = {}) {
		this.loading=true;
		DispatchSetAPI.doSearch(params).then(data => {
			this.hasRight = data.hasRight;
			if(data.hasRight){
				this.tableStore.getDatas(data.sessionkey);
				this.sessionkey = data.sessionkey;
			}
			this.loading=false;
		});
		this.loading=false;
	}

	@action getRightMenu(params = {}) {
        DispatchSetAPI.getRightMenu(params).then(data => {
            this.rightMenu = data.rightMenus;
        });
        return this;
    }

	@action getConditionInfo(params = {}) {
		this.conditionLoading = true;
		DispatchSetAPI.getConditionInfo(params).then(data => {
			this.conditionLoading = false;
			const { isFormInit } = this.form;
			if (isFormInit) {
				// 如果初始化过表单，需要重新初始化一份表单，并在同一周期置空数据
				// 需要暂存数据的可以考虑多份 ComsStore 继承
				this.form = new WeaForm();
				this.conditioninfo = [];
			}
			this.conditioninfo = data.condition;
			this.form.initFormFields(data.condition);
			const keys = Object.keys(params);
			if (keys.length > 1 && this.form.isFormInit) {
				const fields = {};
				keys.forEach(key => {
					if (key !== 'viewcondition' && key !== 'groupid') {
						fields[key] = { value: params[key] };
					}
				});
				this.form.updateFields(fields);
			}
		});
		return this;
	}

	@action setShowSearchAd = showSearchAd => {
		this.showSearchAd = showSearchAd;
	}

	@action setQuickSearchValue = quickSearchValue => {
		this.quickSearchValue = quickSearchValue;
	}

	@action setVisible = visible => {
        this.visible = visible;
    }

	@action setVisible1 = visible1 => {
		this.visible1 = visible1;
	}

	@action setRuleid = ruleid => {
		this.ruleid = ruleid;
	}

	@action setRuledetailid = ruledetailid => {
		this.ruledetailid = ruledetailid;
	}

    @action setDialogType = dialogType => {
        this.dialogType = dialogType;
        if("onNewRule"==dialogType){
			this.ruleid = "";
			this.ruledetailid = "";
			this.getRuleDetailList();
		}
		//新增规则或者编辑规则
		this.getchooseDialogCon(dialogType);
    }

	@action setDialogTypeDetail = dialogTypeDeatil => {
		this.dialogTypeDeatil = dialogTypeDeatil;
		if("addRuleDetail"==dialogTypeDeatil){
			this.ruledetailid = "";
		}
		//新增规则或者编辑规则
		this.getchooseDialogConDetail(dialogTypeDeatil);
	}

    //新增规则或者编辑规则
    @action getchooseDialogCon = params => {
        DispatchSetAPI.getchooseDialogCon({dialogType:params,ruleid:this.ruleid}).then(data => {
            this.chooseDialogCon1 = data.conditioninfo;
            this.form1.initFormFields(data.conditioninfo);
            this.dialogtitle = data.dialogtitle;
            this.chooseDialogRM1 = data.rightMenu;
            if(data.sessionkey1){
				this.tableStore1.getDatas(data.sessionkey1);
				this.sessionkey1 = data.sessionkey1;
			}
        })
    }

	//新增规则明细或者编辑规则明细
	@action getchooseDialogConDetail = params => {
		DispatchSetAPI.getchooseDialogCon({dialogType:params,ruledetailid:this.ruledetailid}).then(data => {
			this.chooseDialogCon2 = data.conditioninfo;
			this.form2.initFormFields(data.conditioninfo);
			this.dialogtitle1 = data.dialogtitle;
			this.chooseDialogRM2 = data.rightMenu;
		})
	}

    //获取规则明细列表
	@action getRuleDetailList = params => {
		DispatchSetAPI.getRuleDetailList({ruleid:this.ruleid}).then(data => {
			this.tableStore1.getDatas(data.sessionkey1);
			this.sessionkey1 = data.sessionkey1;
		})
	}

	//保存规则数据
	@action saveDialog1(params={}){
		DispatchSetAPI.saveDialog1(params).then(data =>{
			this.doSearch();
			this.setRuleid(data.ruleid);
		});
	}

	//保存规则明细数据
	@action saveDialog2(params={}){
		DispatchSetAPI.saveDialog1(params).then(data =>{
			this.tableStore1.getDatas(data.sessionkey1);
			this.sessionkey1 = data.sessionkey1;
		});
	 }

	//删除规则
	 @action deleteRule(params={}){
		DispatchSetAPI.deleteRule(params).then(action(data =>{
			this.doSearch();
		}));
	 }

	//删除规则明细
	@action deleteRuleDetail(params={}){
		DispatchSetAPI.deleteRule(params).then(action(data =>{
			this.tableStore1.getDatas(data.sessionkey1);
			this.sessionkey1 = data.sessionkey1;
		}));
	}


}
